<!--
* @Description: 客户派单信息表
* @Author: ZHe
* @Date: 2022/01/01
* @params:
  {
  }
* @func:
-->
<script lang="jsx">
	import { globalStore } from '@/store'
	import downloadUtil from '@/utils/downloadUtil'
	import customerDispatchApi from '@/api/biz/customerDispatchApi'
	import contractApi from '@/api/biz/bizContractApi'

	const store = globalStore()

	export default {
		components: {},
		props: {
			// 合同信息
			contractInfo: {
				type: Object,
				default: () => ({})
			}
		},
		emits: ['finished'],
		data() {
			return {
				formData: {
					sellName: null,
					team: null,
					serviceCity: null,
					customerName: null,
					customerContactPerson: null,
					contactInformation: null,
					customerWechat: null,
					otherContact: null,
					otherContactInformation: null,
					customerSn: null,

					isRegister: null,
					registerCost: null,
					isEngraved: null,
					engravedCost: null,
					isHasAddress: null,
					addressCost: null,
					legalPersonInformation: null,
					shareholderInformation: null,
					reserveCustomerName: null,
					businessScope: null,
					registeredCapital: null,
					isToPay: null,

					customerNature: null,
					serviceCycle: null,
					firstFilingMonth: null,
					beforeAccountPosition: null,
					isHandOver: null,
					handOverName: null,
					handOverTime: null,
					handOverIdentity: null,
					customerBankingSituation: null,
					isBankAccount: null,
					isPullSlip: null,
					isSocialSecurityFund: null,
					socialSecurityFundInfo: null,

					doWhereAptitude: null,
					hasHandlingConditions: null,
					qualificationRequest: null,
					aptitudeEndTime: null,
					specialSupport: null,
					customerNowInfo: null,
					qualificationManagementRemark: null,

					contractPrice: null,
					serviceInfo: null,
					customerInformationAccountingRequirements: null,
					remark: null,
					totalPrice: null,

					id: null // 编辑用
				}
			}
		},
		computed: {
			renderFormData() {
				return [
					{
						title: '基本情况',
						labelWidth: null,
						children: [
							{ label: '销售', prop: 'sellName', maxlength: 20 },
							{ label: '所属团队', prop: 'team', maxlength: 20 },
							{ label: '服务城市', prop: 'serviceCity', maxlength: 20 },
							{ label: '公司名称', prop: 'customerName', maxlength: 50 },
							{ label: '客户对接人', prop: 'customerContactPerson', maxlength: 20 },
							{ label: '联系方式', prop: 'contactInformation', maxlength: 20 },
							{ label: '客户微信号', prop: 'customerWechat', maxlength: 20 },
							{ label: '其他联系人', prop: 'otherContact', maxlength: 20 },
							{ label: '联系方式', prop: 'otherContactInformation', maxlength: 20 },
							{ label: '客户编号', prop: 'customerSn', maxlength: 20 }
						]
					},
					{
						title: '注册情况',
						labelWidth: '110px',
						children: [
							{ label: '是否需要注册', prop: 'isRegister', maxlength: 20 },
							{ label: '注册成本', prop: 'registerCost', maxlength: 20 },
							{ label: '是否需要刻章', prop: 'isEngraved', maxlength: 20 },
							{ label: '刻章成本', prop: 'engravedCost', maxlength: 20 },
							{ label: '是否需要提供地址', prop: 'isHasAddress', maxlength: 50 },
							{ label: '地址费用', prop: 'addressCost', maxlength: 20 },
							{ label: '法人信息', prop: 'legalPersonInformation', maxlength: 20 },
							{ label: '股东信息，股权比例', prop: 'shareholderInformation', maxlength: 50 },
							{ label: '公司名称备用', prop: 'reserveCustomerName', maxlength: 50 },
							{ label: '经营范围', prop: 'businessScope', maxlength: 500 },
							{ label: '注册资本', prop: 'registeredCapital', maxlength: 20 },
							{ label: '是否实缴，认缴缴纳年限', prop: 'isToPay', maxlength: 20 }
						]
					},
					{
						title: '税务情况',
						labelWidth: '110px',
						children: [
							{ label: '公司性质', prop: 'customerNature', maxlength: 20 },
							{ label: '服务周期', prop: 'serviceCycle', maxlength: 20 },
							{ label: '首次报税月', prop: 'firstFilingMonth', maxlength: 20 },
							{ label: '客户之前账目情况', prop: 'beforeAccountPosition', maxlength: 50 },
							{ label: '是否需要交接', prop: 'isHandOver', maxlength: 20 },
							{ label: '交接对接人', prop: 'handOverName', maxlength: 20 },
							{ label: '交接时间', prop: 'handOverTime', maxlength: 20 },
							{ label: '交接身份', prop: 'handOverIdentity', maxlength: 20 },
							{ label: '客户银行情况', prop: 'customerBankingSituation', maxlength: 50 },
							{ label: '是否需要银行开户', prop: 'isBankAccount', maxlength: 20 },
							{ label: '是否需要拉回单', prop: 'isPullSlip', maxlength: 20 },
							{ label: '社保公积金是否开户', prop: 'isSocialSecurityFund', maxlength: 20 },
							{ label: '社保公积金是否需要处理，具体处理需求', prop: 'socialSecurityFundInfo', maxlength: 50 }
						]
					},
					{
						title: '资质办理',
						labelWidth: '',
						children: [
							{ label: '办理何种资质', prop: 'doWhereAptitude', maxlength: 50 },
							{ label: '客户现有办理条件', prop: 'hasHandlingConditions', maxlength: 50 },
							{ label: '资质办理诉求', prop: 'qualificationRequest', maxlength: 50 },
							{ label: '资质需要的截止时间', prop: 'aptitudeEndTime', maxlength: 20 },
							{ label: '需要我方提供的特殊支持', prop: 'specialSupport', maxlength: 50 },
							{ label: '客户现在情况详细描述', prop: 'customerNowInfo', maxlength: 50 },
							{ label: '备注', prop: 'qualificationManagementRemark', maxlength: 200 }
						]
					},
					{
						title: '签约信息',
						labelWidth: '',
						children: [
							{ label: '签约价格', prop: 'contractPrice', maxlength: 50 },
							{ label: '服务明细', prop: 'serviceInfo', maxlength: 50 },
							{ label: '客户情况及账务需求', prop: 'customerInformationAccountingRequirements', maxlength: 200 },
							{ label: '备注', prop: 'remark', maxlength: 200 },
							{ label: '总计', prop: 'totalPrice', maxlength: 20 }
						]
					}
				]
			},
			isEdit() {
				return this.formData.id
			},
			contractId() {
				return this.contractInfo.id
			}
		},
		watch: {},
		async created() {
			const data = await this.getInfo()
			data &&
				Object.keys(this.formData).forEach((key) => {
					this.formData[key] = data[key]
				})

			this.formData.customerSn = await contractApi.getInnerCode({ customerCode: this.contractInfo.customerCode })

			const targetServiceData = this.contractInfo.list.find(
				(item) => item.serviceClassId / 1 === 54 && item.serviceClassName === '代账服务'
			) // 代账服务
			if (targetServiceData) {
				this.formData.serviceCycle = parseInt(targetServiceData.serviceLife) + parseInt(targetServiceData.giveMonth)
				this.formData.firstFilingMonth = targetServiceData.serviceStime
			}

			// 默认值
			if (!this.isEdit) {
				this.formData.sellName = store.userInfo.name
				this.formData.team = store.userInfo.departmentName
				this.formData.serviceCity = this.contractInfo.firstPartyAddress
				this.formData.customerName = this.contractInfo.firstParty
				this.formData.customerContactPerson = this.contractInfo.firstPartyPerson
				this.formData.contactInformation = this.contractInfo.firstPartyMobile

				this.formData.contractPrice = this.contractInfo.contractAmount
				this.formData.serviceInfo = this.contractInfo.list
					?.map((item) => item.serviceClassName + '>' + item.serviceTypeName + '>' + item.serviceProjectName)
					.join('，')
				this.formData.totalPrice = this.contractInfo.contractAmount
			}
		},
		methods: {
			save(isDownload) {
				const params = {
					createUserId: store.userInfo.id,
					contractId: this.contractId,
					...this.formData
				}
				const requestFn = this.isEdit ? customerDispatchApi.updateDispatch : customerDispatchApi.addDispatch
				requestFn(params).then((res) => {
					this.$message.success(isDownload ? '派单成功，将为您下载信息表！' : '派单成功！')
					this.$emit('finished')
					if (!this.isEdit && res) this.formData.id = res

					isDownload && this.download()
				})
			},
			// 下载
			download() {
				customerDispatchApi
					.exportDispatch({
						contractId: this.contractId
					})
					.then((res) => {
						downloadUtil.resultDownload(res, '客户派单信息表.xls')
					})
			},
			// 获取详情
			getInfo() {
				return customerDispatchApi.dispatchDetail({ contractId: this.contractId })
			}
		},

		render() {
			const formEl = (
				<el-form
					ref="form"
					label-width="280px"
					props={{
						model: this.formData
					}}>
					{this.renderFormData.map((item) => {
						const itemEl = item.children.map((childItem) => (
							<el-form-item prop={childItem.prop} label={childItem.label} maxlength={childItem.maxlength}>
								<el-input style="width: 98%;" {...this.$attrs} v-model={this.formData[childItem.prop]}></el-input>
							</el-form-item>
						))

						return [<div className="sub-title">{item.title}</div>, itemEl]
					})}
				</el-form>
			)

			return (
				<div className="customerScheduling-component">
					{this.formData.sellName ? formEl : null}
					<div style="text-align:center">
						<el-button type="primary" onClick={() => this.save(true)}>
							{' '}
							保存并下载{' '}
						</el-button>
						<el-button type="primary" onClick={() => this.save()}>
							{' '}
							保 存{' '}
						</el-button>
					</div>
				</div>
			)
		}
	}
</script>
<style lang="less">
	.customerScheduling-component {
		height: 75vh;
		overflow: auto;
		.sub-title {
			font-weight: 600;
			margin-bottom: 10px;
		}
	}
</style>
